package cian.service.impl;

import cian.dao.UserMapper;
import cian.entity.User;
import cian.filter.SystemContext;
import cian.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Cian on 2017/4/26.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    public User findUserByUserName(String username) {
        return userMapper.findUserByUserName(username);
    }

    public List<User> findAll() {
        return userMapper.findAllUsers();
    }

    public int deleteByName(String username) {
        int column = userMapper.deleteByName(username); //执行方法后影响的行数为i

        if (column == 1){
            return  1;  //删除成功返回1
        }
        return -1;  //删除失败返回-1
    }

    public int add(User user) {
        int i = userMapper.add(user);   //插入用户后的返回结果为i
        if (i == 0){
            return 1;   //插入成功，返回1
        }
        return -1;  //插入失败，返回-1
    }

//    public List<User> test() {
//        return userMapper.test();
//    }


    public PageInfo<User> pageHelper(String username, Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null?1:pageNo;   //判断当前页数是否存在，不存在就等于1
        pageSize = pageSize == null?10:pageSize;    //判断当前每页显示信息条数，默认为10
        PageHelper.startPage(pageNo, pageSize);
//        System.out.println("-------------pagno"+pageNo);
//        System.out.println("-------------pagsz"+pageSize);
        int var1 = (pageNo-1)*pageSize+1;
        int var2 = pageNo*pageSize;
//        System.out.println("-------------var1:"+var1);
//        System.out.println("-------------var2:"+var2);
        List<User> list = userMapper.test(var1,var2);
        //用PageInfo对结果进行包装
        PageInfo<User> page = new PageInfo<User>(list);

        page.setList(list);
        page.setPageNum(pageNo);
        page.setPageSize(pageSize);

        //测试PageInfo全部属性
//        System.out.println(page.getPageNum());
//        System.out.println(page.getPageSize());
//        System.out.println("list size is "+ page.getList().size());
        return page;
    }

    public List<User> test() {
        return null;
    }

    public int deleteByUserName(String username) {
        return userMapper.deleteByName(username);
    }


    public int modifyByUserName(User user) {
        int i = userMapper.modifyByUserName(user);
        return i;
    }

}
